package cn.tedu.boot522.controller;

import cn.tedu.boot522.entity.User;
import cn.tedu.boot522.entity.Weibo;
import cn.tedu.boot522.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.security.PublicKey;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class WeiboController {
    @Autowired(required = false)
    WeiboMapper mapper;

    @RequestMapping("/insert")
    public int insert(@RequestBody Weibo weibo, HttpSession session){
        User u = (User) session.getAttribute("user");
        if(u==null){
            return 2;
        }
        weibo.setNick(u.getNick());
        weibo.setUserId(u.getId());
        weibo.setCreated(new Date());
        mapper.insert(weibo);
        return 1;
    }

    @RequestMapping("/select")
    public List<Weibo> select(){
        return mapper.select();
    }

    @RequestMapping("/selectByWeiboId")
    public Weibo selectByWeiboId(int id){
        return mapper.selectByWeiboId(id);
    }

    @RequestMapping("/self/select")
    public List<Weibo> selectSelf(HttpSession session){
        User u = (User) session.getAttribute("user");
        if(u==null){
            return null;
        }
        return mapper.selectSelf(u.getId());

    }

    @RequestMapping("/delete")
    public void delete(int id){
        mapper.delete(id);
    }
}
